package com.master_jpeg.visual;

import com.master_jpeg.img.ImagePanel;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;


public class MainPanel extends JPanel {
    private static final String URL_PATH = "D:\\My projects\\magister-jpeg\\master-jpeg\\123123.png";
    private static final int TIMER_DELAY = 10;
    private JScrollPane scrollpane;
    private ImagePanel imagePanel;

    public MainPanel() {
        BufferedImage img = null;
        try {
            File imageUrl = new File(URL_PATH);
            img = ImageIO.read(imageUrl);
        } catch (MalformedURLException e) {
            e.printStackTrace();
            System.exit(-1);
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(-1);
        }

        imagePanel = new ImagePanel(img);
        scrollpane = new JScrollPane(imagePanel);
        scrollpane.getViewport().setScrollMode(1);
        setLayout(new BorderLayout());
        add(scrollpane, BorderLayout.CENTER);

        new Timer(TIMER_DELAY, new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                scrollpane.getViewport().revalidate();  // **** un-comment this line ****
            }
        }).start();

    }

}
